<template>
    <div class="listDiv">
        <div class="searchDiv">
            <el-select v-model="obj.req.priority" placeholder="优先级" class="search_select" clearable>
                <el-option v-for="(item,index) in globalData.priorityList" :key="index" :label="item.text" :value="item.value" />
            </el-select>
            <el-select v-model="obj.req.state" placeholder="状态" class="search_select" clearable>
                <el-option v-for="(item,index) in globalData.timestateAry" :key="index" :label="item.text" :value="item.value" />
            </el-select>
            <el-input clearable placeholder="匹配名称" v-model="obj.req.key" class="search_key" />
            <span class="btn search" @click="init">搜索</span>
            <span class="btn refresh" @click="obj.req.currentpage=1;init()"></span>
            <div class="searchRight">
                <span class="btn add" @click="add">添加工单</span>
            </div>
        </div>
        <div class="flex-row-center mb5">
            <span class="mini-size mr5">安保工单完成率:</span>
            <span class="mini-size mr10">{{obj.qualified_security0||0}}%</span>
            <el-input-number clearable placeholder="考勤合格率" v-model="obj.qualified_security" :min="0" :max="100"  />
            <span class="btn add ml5" @click="qualified_security_design">修改</span>
        </div>
        <div class="table_box"><div class="table_" id="tb">
            <ul class="head">
                <li class="wcen mw130">工单编号</li>
                <li class="mw300">任务主题</li>
                <li class="">操作员</li>
                <li class="mw200 wcen">开始时间</li>
                <li class="mw200 wcen">截止时间</li>
                <li class="wcen">状态</li>
                <li class="wcen">优先级</li>
                <li class="mw200 wcen">操作</li>
            </ul>
            <div class="body">
                <ul v-for="x in obj.ary" :key="x">
                    <li class="mw130 wcen">{{x.code}}</li>
                    <li class="mw300">
                        <el-tooltip class="box-item" effect="dark" :content="x.title" placement="top-start" :disabled="x.title.length<17">
                            <div class="lineOne pointer">{{x.title}}</div>
                        </el-tooltip>
                    </li>
                    <li class="">{{x.username}}</li>
                    <li class="mw200 wcen">{{x.starttime}}</li>
                    <li class="mw200 wcen">{{x.endtime}}</li>
                    <li class="wcen">
                        <div :class="[fix(x).label]">{{fix(x).text||''}}</div>
                    </li>
                    <li class="wcen">
                        <div :class="[proxy.$utils.find(globalData.priorityList, x.priority, 'label')]">{{proxy.$utils.find(globalData.priorityList, x.priority)}}</div>
                    </li>
                    <li class="mw200 wcen">
                        <el-button class="edit_btn mr10" type="success" @click="edit(x)">修改</el-button>
                        <DelConfirm class="" @yesClick="del(x)" />
                    </li>
                </ul>
            </div>
        </div>
        </div><Pager :value="obj.req" :init="init" />

        <el-dialog v-model="obj.drawer.design" :title="obj.drawer.designTitle" width="1000px" :close-on-click-modal="false" :destroy-on-close="true">
            <router-view @close="obj.drawer.design = false" @init="init" />
        </el-dialog>
    </div>
</template>

<script setup>
    import { reactive, getCurrentInstance, ref, toRefs, provide, computed } from 'vue'
    import Pager from '@/components/pager.vue'
    import DelConfirm from '@/components/del_confirm.vue'
    let { proxy } = getCurrentInstance()

    let obj = reactive({
        drawer: {
            design: false,
            designTitle: "",
        },
        ary: [],
        req: {
            currentpage: 1,
            pagesize: 10,
            count: 0,
            key: "",
            priority:"",
            state: ''
        },
        security_sign0: 0,
        security_sign: 0,
    });
    let init = () => {
        proxy.$ajax({
            url: '/security/list',
            data: obj.req,
            bfLoading: true
        }).then(d => {
            // console.log(d)
            if(d.success){
                obj.ary = d.msg.list
                obj.req.count = d.msg.count
            }
        }).catch(() => { })

        qualified_security_get()
    }

    let qualified_security_get = ()=>{
        proxy.$ajax({
            url: '/material/qualified_security_get',
            data: {},
            bfLoading: true
        }).then(d => {
            if(d.success){
                obj.qualified_security0 = +d.msg||0
                obj.qualified_security =  +d.msg||0
            }
        }).catch(() => { })
    }

    let qualified_security_design = ()=>{
        if(obj.qualified_security===''){
            proxy.elMessageMixin('请输入合格率', 'error')
            return
        }
        proxy.$ajax({
            url: '/material/qualified_security_design',
            data: {qualified_security:obj.qualified_security},
            bfLoading: true
        }).then(d => {
            if(d.success){
                proxy.elMessageMixin("修改成功", 'success')
                qualified_security_get()
            }else{
                proxy.elMessageMixin(d.err, 'error')
            }
        }).catch(() => { })
    }
    let fix = computed(() => {
        return (x) => {
            if(x.complete == 1) return {text:"完成",color:'success-color',label:'label-btn-success'}
            let start = new Date(x.starttime), end = new Date(x.endtime), now = new Date()
            if (start > now) return {text:"尚未开始",color:'gray-color',label:'label-btn-warning'}
            if (end < now) return {text:"已结束",color:'gray-color',label:'label-btn-info'}
            return {text:"进行中",color:'base-color',label:'label-btn-primary'}
        }
    })

    let editId = ref(0)
    provide("id", editId); //不能加.value
    let add = () => {
        obj.drawer.design = true
        obj.drawer.designTitle = "添加安保工单"
        editId.value = 0;
    }
    let edit = x => {
        obj.drawer.design = true
        obj.drawer.designTitle = "修改安保工单"
        editId.value = x.work_id;
    }

    let del = x => {
        proxy.$ajax({
            url: '/security/delete',
            data: { work_id: x.work_id },
            bfLoading: true
        }).then(d => {
            // console.log(d)
            if(d.success){
                proxy.elMessageMixin("删除成功", 'success')
                init()
            }else{
                proxy.elMessageMixin(d.err, 'error')
            }
        }).catch(() => { })
    }
</script>

<style scoped>
    ::-webkit-scrollbar { width:10px; background-color: #FFFFFF; }
    .search_select { width:100px!important; }
    .search_key { width:160px!important; }
    #tb ul li.li1 { width: 120px; }
    #tb ul li.li2 { width: 75px; }
    #tb ul li.li5 { width: 75px; }
    #tb ul li.li6 { width: 55px; }
    #tb ul li.li7 { width: 75px; }
</style>